package search;

import exception.PointOutOfBoundsException;
import treeview.NodeView;
import treeview.container.HorizontalContainer;
import treeview.container.VerticalContainer;
import treeview.table.TableView;
import treeview.unit.AbstractUnitView;
import treeview.unit.ImageView;
import treeview.unit.TextView;

/**
 * Created by Pavel Nikolaev
 */
public class XYLocalizer implements Visitor {
    private int myLookedForX = 0;
    private int myLookedForY = 0;
    private AbstractUnitView myLocalizedUnitView = null;


    public AbstractUnitView searchByCoordinates(NodeView root, int x, int y) throws PointOutOfBoundsException {
        myLookedForX = x;
        myLookedForY = y;
        root.accept(this);
        if (myLocalizedUnitView == null)
            throw new PointOutOfBoundsException(x, y);
        return myLocalizedUnitView;
    }

    public void visitTextView(TextView view) {
        if (view.intersect(myLookedForX, myLookedForY)) myLocalizedUnitView = view;
    }

    public void visitImageView(ImageView view) {
        if (view.intersect(myLookedForX, myLookedForY)) myLocalizedUnitView = view;
    }

    public void visitUnitView(AbstractUnitView view) {
        if (view.intersect(myLookedForX, myLookedForY)) myLocalizedUnitView = view;
    }

    public boolean visitHorizontalContainer(HorizontalContainer view) {
        return view.intersect(myLookedForX, myLookedForY);
    }

    public boolean visitVerticalContainer(VerticalContainer view) {
        return view.intersect(myLookedForX, myLookedForY);
    }

    public boolean visitTableView(TableView view) {
        return view.intersect(myLookedForX, myLookedForY);
    }
}